<?php
/**
 *
 * @date 2016年8月23日 13:37:33
 * @author Earl
 *
 */
namespace Advert\Controller;
use Think\Upload;
use Admin\Controller\CommonController;
use Advert\Model\AdvertModel;
class AdvertController extends CommonController {
	protected $advert_db;
	function _initialize(){
		parent::_initialize();
		$this->advert_db = new AdvertModel();
	}
    /**
     * 广告列表
     * @param int $page
     * @param int $rows
     * @param array $search
     *
     */
	function index($page=1, $rows=10, $search=array()){

        if (IS_POST) {
            $map = 'type =10';
            $limit = ($page - 1) * $rows . "," . $rows;
            $total = $this->advert_db->where($map)->count();
            $list  = $total ? $this->advert_db->_list($map, $limit,'updatetime desc') : array();
            if ($total) {
                foreach ($list as $k=>$v){
                    $list[$k]['createtime'] = date('Y-m-d H:i',$v['createtime']);
                    $list[$k]['updatetime'] = date('Y-m-d H:i',$v['updatetime']);
                }
            }
            $this->ajaxReturn(array('total'=>$total, 'rows'=>$list));
        }else {
            $this->currentpos = $this->menu_db->currentPos(I('menuid'));  //栏目位置
            $this->display('list');
        }
	}

    function immediate($page=1, $rows=10, $search=array()){
        if (IS_POST) {
            $map = 'type =20';
            $limit = ($page - 1) * $rows . "," . $rows;
            $total = $this->advert_db->where($map)->count();
            $list  = $total ? $this->advert_db->_list($map, $limit,'updatetime desc') : array();
            if ($total) {
                foreach ($list as $k=>$v){
                    $list[$k]['createtime'] = date('Y-m-d H:i',$v['createtime']);
                    $list[$k]['updatetime'] = date('Y-m-d H:i',$v['updatetime']);
                }
            }
            $this->ajaxReturn(array('total'=>$total, 'rows'=>$list));
        }else {
            $this->currentpos = $this->menu_db->currentPos(I('menuid'));  //栏目位置
            $this->display('list2');
        }
    }

    /**
     * 添加
     */
    function advertAdd(){
        if (IS_POST) {
            $data = I('info');
            $data['type'] = 10;
            $data['createtime'] = SYS_TIME;
            $data['updatetime']=SYS_TIME;
            if ($this->advert_db->add($data)) {
                $this->success('添加成功');
            }else {
                $this->error('添加失败');
            }
        }else {
            $this->display('advert_add');
        }
    }

    function advertImmeAdd(){
        if (IS_POST) {
            $data = I('info');
            $data['type'] = '20';
            $data['updatetime']=SYS_TIME;
            $data['createtime'] = SYS_TIME;
            if ($this->advert_db->add($data)) {
                $this->success('添加成功');
            }else {
                $this->error('添加失败');
            }
        }else {
            $this->display('advert_imme_add');
        }
    }

    /**
     * @param int $id
     * 删除
     */
    function advertDelete($id =0){
        if ($this->advert_db->delete($id)) {
            $this->success('删除成功');
        }else {
            $this->error('删除失败');
        }
    }

    /**
     * @param int $id
     * 编辑
     */
    function advertEdit($id=0){

        if (IS_POST) {
            $data = I('info');
            $data['updatetime']=SYS_TIME;
            if ($this->advert_db->save($data) !== false) {
                $this->success('编辑成功');
            }else {
                $this->error('编辑失败');
            }
        }else {
            $this->info = $this->advert_db->find($id);
            $this->display('advert_edit');
        }
    }

    function advertImmeEdit($id=0){

        if (IS_POST) {
            $data = I('info');
            $data['updatetime']=SYS_TIME;
            if ($this->advert_db->save($data) !== false) {
                $this->success('编辑成功');
            }else {
                $this->error('编辑失败');
            }
        }else {
            $this->info = $this->advert_db->find($id);
            $this->display('advert_imme_edit');
        }
    }

    /**
     * 图片上传处理
     */
    function public_upload(){
        $URL_PATH = SITE_PROTOCOL.SITE_URL.UPLOADS;//物理根目录
        $upload = new Upload();
        $upload->exts = array('gif','jpg','jpeg','png');// 设置附件上传类型
        $upload->rootPath  = SITE_DIR.UPLOADS;
        $upload->savePath  = '/Picture/'; // 设置附件上传目录
        $upload->subName   = array('date','ymd');
        $upload->replace   = true;
        // 上传文件
        $info   =   $upload->upload();
        if(!$info) {// 上传错误提示错误信息
            $this->ajaxReturn(array('info'=>$upload->getError(),'status'=>0));
        }else{// 上传成功
            foreach($info as $file){
                $url =  $file['savepath'].$file['savename'];
            }
            $this->ajaxReturn(array('info'=>'上传成功', 'url'=>$URL_PATH.ltrim($url,'/'), 'status'=>1));
        }
    }
}